import 'dart:convert';

import 'package:uuid/uuid.dart';

import '../../util/local/local.dart';


class ValueListItem{
  String? name;
  // 	静态值 | 动态值 |常量值 | 操作值
  String? type;
}


class TemplateData {
  // 名称
  String? name;
  // 描述
  String? desc;
  // 标识
  String? id;

  List<dynamic>? valueListItem;

  TemplateData({this.name, this.desc, this.id, this.valueListItem});

  TemplateData.fromJson(Map<String, dynamic> json)
      : name = json['name'] as String,
        desc = json['desc'] as String,
        valueListItem = json['valueListItem'] != null
            ? json['valueListItem'] as List<dynamic>
            : null,
        id = json['id'] as String;

  Map<String, dynamic> toJson() => {
        'name': name,
        'desc': desc,
        'id': id,
        'valueListItem': valueListItem,
      };
}

class TemplateStore {
  static LocalStorage store = const LocalStorage(key: 'template');
  static const uuid = Uuid();

  static Object data = {};
  static insertTemplate(TemplateData data) async {
    data.id = uuid.v4();
    List<TemplateData> list = await TemplateStore.getTemplateList();
    List<Map<String, dynamic>> encodeData = list.map((item) {
      return item.toJson();
    }).toList();
    encodeData.add(data.toJson());
    String s = json.encode(encodeData);
    await store.setString('list', s);
  }

  static Future<List<TemplateData>> getTemplateList() async {
    String? s = await store.getString('list');
    if (s != null) {
      List<dynamic> list = json.decode(s);
      List<TemplateData> dataList = list.map((item) {
        return TemplateData.fromJson(item);
      }).toList();
      return dataList;
    }
    return [];
  }
}
